<?php
namespace Admin\Controller;

use Util\AccessController;

class BannerController extends AccessController {
    public function index() {
        $where['banner_url_type'] = $_GET['banner_url_type'];
        $banner_name = '广告图';
        switch ($_GET['banner_url_type']) {
            case 1:
                $banner_name = '主题书单横幅';
                break;
            case 4:
                $banner_name = '轮播图横幅';
                break;
        }
        $LS = Service('Banner', 'banner');
        $order = 'banner_number DESC';
        $LBdata = $LS->query($where,null,null,$order);
        $this->banner_name = $banner_name;
        $this->LBDATA = $LBdata;
        $this->display();
    }

    public function addPic() {
        $LS = Service('Banner', 'banner');
        $data = $LS->find(array('banner_id' => $_GET['banner_id']));
        $this->data = $data;
        $this->display();
    }

    public function Addhanld() {
        $LS = Service('Banner', 'banner');
        $data = $_POST;

        if ($data['banner_id']) {
            $where['banner_id'] = $_POST['banner_id'];
            $save = $data;
            $_POST['picpath'][0] ? $save['banner_img'] = $_POST['picpath'][0]: 1;
            unset($save['picpath']);
            $save['banner_time'] = date('Y-m-d H:i:s');
            $OK = $LS->update($where, $save);
        } else {
            $_POST['picpath'][0] ? $data['banner_img'] = $_POST['picpath'][0]: 1;
            unset($data['picpath']);
            $data['banner_id'] = 'ban_' . uniqid() . rand(0, 9);
            $data['banner_time'] = date('Y-m-d H:i:s');
            $OK = $LS->insert($data);
        }
        $OK ? $this->success('编辑成功') : $this->error('编辑失败');
    }

    public function Del() {
        $LS = Service('Banner', 'banner');
        $OK = $LS->remove(array('banner_id' => $_POST['ID']));
        echo $OK;
    }


}